jQuery(document).ready(function(){	
	
	jQuery('#btnSubmitList').click(function(){				
		showAjaxEffect();		
	});	
	
	jQuery('.paginate_button').click(function(){				
		showAjaxEffect();		
	});	
	
	jQuery('#applyTube').click(function(){					
		var action = jQuery('#action').val();
		if(action=='0'){
			alert('Please choose action !');
		}else {
			var ids = '';
			var count = 0;
			jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
				if (jQuery(this).is(':checked')) {				
					ids += jQuery(this).val() + ',';
					count++;
				}
			});		
			if(count>0){
				if(action=='1'){
					if(confirm('All checked tubes will be delete !')){						
						showAjaxEffect();
						jQuery.get('/admin/tube/process', {ids: ids, action: 'delete-all'}, function(data) {
							hideAjaxEffect();
							scrollToTop();
							if(data==''){
								jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
									if (jQuery(this).is(':checked')) {				
										jQuery(this).parents("tr").remove();						
									}
								});
								jQuery('#messageDiv').fadeIn();
								jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
								if(jQuery('#table-result tr').length <= 7){
									jQuery('#messageDiv > p').html('Action success ! Tubes have been deleted. Loading new tubes...');
									setTimeout(function() {
										window.location.href = window.location.href;
									}, 2000);	
								}else{
									jQuery('#messageDiv > p').html('Action success ! Tubes have been deleted');
									setTimeout(function() {
										jQuery('#messageDiv').fadeOut();
									}, 2000);	
								}																
							}else{
								jQuery('#messageDiv').fadeIn();
								jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
								jQuery('#messageDiv > p').html(data);
								setTimeout(function() {
									jQuery('#messageDiv').fadeOut();
								}, 2000);	
							}							
						});
					}else{
						return false;
					}	
				}else if(action=='2'){
					if(confirm('All checked tubes will be add to slideshow !')){
						showAjaxEffect();
						jQuery.get('/admin/tube/process', {ids: ids, action: 'slideshow-all'}, function(data) {
							hideAjaxEffect();	
							scrollToTop();
							if(data==''){			
								jQuery('#messageDiv').fadeIn();
								jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
								jQuery('#messageDiv > p').html('Action success ! Tubes have been added to slideshow');
								setTimeout(function() {
									jQuery('#messageDiv').fadeOut();
								}, 2000);	
							}else{
								jQuery('#messageDiv').fadeIn();
								jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
								jQuery('#messageDiv > p').html(data);
								setTimeout(function() {
									jQuery('#messageDiv').fadeOut();
								}, 2000);	
							}	
						});
					}else{
						return false;
					}	
				}else{
					var plId = action;
					var plTitle = jQuery('#action option:selected').attr('title');
					showAjaxEffect();
					jQuery.get('/admin/tube/process', {ids: ids, plId: plId, action: 'change-playlist'}, function(data) {
						hideAjaxEffect();
						scrollToTop();
						if(data==''){
							jQuery('#table-result tbody tr td input[type=checkbox]').each(function (item, data) {
								if (jQuery(this).is(':checked')) {				
									jQuery(this).parents('tr').find('.playlist-link').html('<a target="blank" href="/admin/playlist/edit?id='+ plId +'" title="View '+ plTitle +' playlist">' + plTitle + '</a>');									
									jQuery(this).attr('checked', false);													
								}
							});
							jQuery('#messageDiv').fadeIn();
							jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
							jQuery('#messageDiv > p').html('Action success ! ' + count + ' videos have been changed playlist !');
							setTimeout(function() {
								jQuery('#messageDiv').fadeOut();
							}, 2000);						
						}else{
							jQuery('#messageDiv').fadeIn();
							jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
							jQuery('#messageDiv > p').html(data);
							setTimeout(function() {
								jQuery('#messageDiv').fadeOut();
							}, 2000);	
						}								
					});	
				}								
			}else{
				alert('Please check some tubes to do this action !');
			}				
		}
	});							
	jQuery('.btnDelete').click(function(){	
		var temp = jQuery(this);
		if(confirm('Are you sure want to delete this tube ?')){				
			showAjaxEffect();
			jQuery.get('/admin/tube/process', { 
				action: 'delete', 
				id: jQuery(this).attr('href'), 
					} ,
				function( data ) {
					hideAjaxEffect();
					scrollToTop();
					if(data==''){							
						temp.parents('tr').remove();	
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
						jQuery('#messageDiv > p').html('Action success ! Tube has been deleted !');
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}else{
						jQuery('#messageDiv').fadeIn();
						jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
						jQuery('#messageDiv > p').html(data);
						setTimeout(function() {
							jQuery('#messageDiv').fadeOut();
						}, 2000);	
					}	
	     		}
			);				
		}
		return false;
	}); 
	jQuery('.btnBanner').click(function(){			
		showAjaxEffect();
		jQuery.get('/admin/tube/process', { 
			action: 'set-slide', 
			id: jQuery(this).attr('href'), 
				} ,
			function( data ) {
				hideAjaxEffect();
				scrollToTop();
				if(data==''){		
					jQuery('#messageDiv').fadeIn();
					jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
					jQuery('#messageDiv > p').html('Action success ! Tube has been added to slideshow');
					setTimeout(function() {
						jQuery('#messageDiv').fadeOut();
					}, 2000);	
				}else{
					jQuery('#messageDiv').fadeIn();
					jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
					jQuery('#messageDiv > p').html(data);
					setTimeout(function() {
						jQuery('#messageDiv').fadeOut();
					}, 2000);	
				}	
     		}
		);	
		return false;
	}); 
	
	jQuery('.btnBump').click(function(){	
		var temp = jQuery(this);
		var currentPage = jQuery('.paginate_active').attr('title');		
		showAjaxEffect();
		jQuery.get('/admin/tube/process', { 
			action: 'bump', 
			id: jQuery(this).attr('href'), 
				} ,
			function( data ) {
				hideAjaxEffect();
				scrollToTop();
				if(data==''){		
					if(currentPage=='1'){
						temp.parents('tr').remove();		
						jQuery('#table-result tbody').prepend('<tr>' + temp.parents('tr').html() + '</tr>');
											
					}else{
						temp.parents('tr').remove();	
					}
					jQuery('#messageDiv').fadeIn();
					jQuery('#messageDiv').addClass('msgsuccess').removeClass('hidden');
					jQuery('#messageDiv > p').html('Action success ! Tube has been up to top');
					setTimeout(function() {
						jQuery('#messageDiv').fadeOut();
					}, 2000);	
				}else{
					jQuery('#messageDiv').fadeIn();
					jQuery('#messageDiv').addClass('msgerror').removeClass('hidden');
					jQuery('#messageDiv > p').html(data);
					setTimeout(function() {
						jQuery('#messageDiv').fadeOut();
					}, 2000);	
				}	
     		}
		);	
		return false;
	}); 
	//	jQuery('#formKeyword').bind('focusin focusout', function(e){
	//		var t = jQuery(this);
	//		if(e.type == 'focusin' && t.val() == 'Type something and hit enter...') {
	//			t.val('');
	//		} else if(e.type == 'focusout' && t.val() == '') {
	//			t.val('Type something and hit enter...');	
	//		}
	//	});
});